<?php

include 'DBconn.php';

class PoAmend {

    public function addNewPoAmend($VObject) {
        $query = "insert into po_amend_header(po_serialnumber_fk,po_amend_id,supplier_serialnumber_fk,po_issue_date,expected_datetime,total_qty,shipment_charge,po_status,discount,tax,total_amt,po_remarks,po_closed_date,cre_userid_fk,cre_datetime,activestatus) values('$VObject->po_serialnumber_fk','$VObject->po_amend_id','$VObject->supplier_serialnumber_fk','$VObject->po_issue_date','$VObject->expected_datetime','$VObject->total_qty','$VObject->shipment_charge','$VObject->po_status','$VObject->discount','$VObject->tax','$VObject->total_amt','$VObject->po_remarks','$VObject->po_closed_date','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
        $s = mysql_query($query) or die(mysql_error());
        if ($s) {
            $last_id = mysql_insert_id();
            foreach ($VObject->enq_serialnumber_fk as $r => $value) {
                $query1 = "insert into enq_amend_po(po_amend_serialnumber_fk,enq_serialnumber_fk,cre_userid_fk,cre_datetime,activestatus) values('" . $last_id . "','" . $value . "','" . $VObject->cre_userid_fk . "','" . $VObject->cre_datetime . "',1)";
                mysql_query($query1) or die(mysql_error());
            }
            foreach ($VObject->pro_serialnumber_fk as $r => $value) {
                $pro_discount = mysql_real_escape_string($VObject->pro_discount[$r]);
                $pro_tax = mysql_real_escape_string($VObject->pro_tax[$r]);
                $pro_qty = mysql_real_escape_string($VObject->pro_qty[$r]);
                $unit_price = mysql_real_escape_string($VObject->unit_price[$r]);
                $pro_total_price = mysql_real_escape_string($VObject->pro_total_price[$r]);
                $query2 = "insert into po_amend_details(po_amend_serialnumber_fk,pro_serialnumber_fk,pro_discount,pro_tax,pro_qty,unit_price,pro_total_price,cre_userid_fk,cre_datetime,activestatus) values('$last_id','$value','$pro_discount','$pro_tax','$pro_qty','$unit_price','$pro_total_price','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
                mysql_query($query2) or die(mysql_error());
            }
            mysql_query("update po_header set isamend=1 where po_serialnumber='$VObject->po_serialnumber_fk'") or die(mysql_error());
        }
        return $this->getPoAmendByCode($last_id);
    }

    // public function editPoAmend($VObject)
//    {
//        //'$VObject->po_id','$VObject->supplier_serialnumber_fk','$VObject->po_issue_date','$VObject->expected_datetime','$VObject->total_qty','$VObject->shipment_charge','$VObject->po_status','$VObject->discount','$VObject->tax','$VObject->total_amt','$VObject->po_remarks','$VObject->po_closed_date',0,'$VObject->cre_userid_fk','$VObject->cre_datetime',1)
//        $query="update po_amend_header set supplier_serialnumber_fk='$VObject->supplier_serialnumber_fk',po_issue_date='$VObject->po_issue_date',expected_datetime='$VObject->expected_datetime',total_qty='$VObject->total_qty',shipment_charge='$VObject->shipment_charge',po_status='$VObject->po_status',discount='$VObject->discount',tax='$VObject->tax',total_amt='$VObject->total_amt',po_remarks='$VObject->po_remarks',po_closed_date='$VObject->po_closed_date',cre_userid_fk='$VObject->cre_userid_fk',cre_datetime='$VObject->cre_datetime' where po_amend_serialnumber='$VObject->po_amend_serialnumber' and po_serialnumber_fk='$VObject->po_serialnumber_fk' and activestatus=1";
//        $s=mysql_query($query)or die(mysql_error());
//        if ($s=1)
//        {
//            mysql_query("update enq_amend_po set activestatus=0 where po_amend_serialnumber_fk='$VObject->po_amend_serialnumber'") or die(mysql_error());
//            mysql_query("update po_amend_details set activestatus=0 where po_amend_serialnumber_fk='$VObject->po_amend_serialnumber'") or die(mysql_error());
//            foreach ($VObject->enq_serialnumber_fk as $r=>$value)
//            {
//                $query1="insert into enq_amend_po(po_amend_serialnumber_fk,enq_serialnumber_fk,cre_userid_fk,cre_datetime,activestatus) values('$VObject->po_amend_serialnumber','$value','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
//                mysql_query($query1) or die(mysql_error());
//            }
//            foreach ($VObject->pro_serialnumber_fk as $r=>$value)
//            {
//                $pro_discount=mysql_real_escape_string($VObject->pro_discount[$r]);
//                $pro_tax=mysql_real_escape_string($VObject->pro_tax[$r]);
//                $pro_qty=mysql_real_escape_string($VObject->pro_qty[$r]);
//                $unit_price=mysql_real_escape_string($VObject->unit_price[$r]);
//                $pro_total_price=mysql_real_escape_string($VObject->pro_total_price[$r]);
//                $query2="insert into po_amend_details(po_amend_serialnumber_fk,pro_serialnumber_fk,pro_discount,pro_tax,pro_qty,unit_price,pro_total_price,cre_userid_fk,cre_datetime,activestatus) values('$VObject->po_amend_serialnumber','$value','$pro_discount','$pro_tax','$pro_qty','$unit_price','$pro_total_price','$VObject->cre_userid_fk','$VObject->cre_datetime',1)";
//                mysql_query($query2) or die(mysql_error());
//            }
//            //foreach ($VObject->enq_serialnumber_fk as $r=>$value)
////            {  
////                $enq_amend_po_serialnumber=mysql_real_escape_string($VObject->enq_amend_po_serialnumber[$r]);
////                $query1="update enq_amend_po set enq_serialnumber_fk='$value',cre_userid_fk='$VObject->cre_userid_fk',cre_datetime='$VObject->cre_datetime' where enq_amend_po_serialnumber='$enq_amend_po_serialnumber' and po_amend_serialnumber_fk='$VObject->po_amend_serialnumber' and activestatus=1";
////                mysql_query($query1) or die(mysql_error());
////            }
////            foreach ($VObject->pro_serialnumber_fk as $r=>$value)
////            {
////                $po_amend_detserialnumber=mysql_real_escape_string($VObject->po_amend_detserialnumber[$r]);
////                $pro_discount=mysql_real_escape_string($VObject->pro_discount[$r]);
////                $pro_tax=mysql_real_escape_string($VObject->pro_tax[$r]);
////                $pro_qty=mysql_real_escape_string($VObject->pro_qty[$r]);
////                $unit_price=mysql_real_escape_string($VObject->unit_price[$r]);
////                $pro_total_price=mysql_real_escape_string($VObject->pro_total_price[$r]);
////                $query2="update po_amend_details set pro_serialnumber_fk=$value,pro_discount=$pro_discount,pro_tax=$pro_tax,pro_qty=$pro_qty,unit_price=$unit_price,pro_total_price=$pro_total_price,cre_userid_fk='$VObject->cre_userid_fk',cre_datetime='$VObject->cre_datetime' where po_amend_detserialnumber='$po_amend_detserialnumber' and po_amend_serialnumber_fk='$VObject->po_amend_serialnumber' and activestatus=1";
////                mysql_query($query2) or die(mysql_error());
////            }
//        }
//        return $this->getPoAmendByCode($VObject->po_amend_serialnumber);
//       
//     } 
    public function deletePoAmend($po_amend_serialnumber) {
        $query = "update po_amend_header set activestatus=0 where po_amend_serialnumber='$po_amend_serialnumber'";
        mysql_query($query) or die(mysql_error());
        $query1 = "update enq_amend_po set  activestatus=0 where po_amend_serialnumber_fk='$po_amend_serialnumber'";
        mysql_query($query1) or die(mysql_error());
        $query2 = "update  po_amend_details set activestatus=0 where po_amend_serialnumber_fk='$po_amend_serialnumber'";
        mysql_query($query2) or die(mysql_error());
        return $po_amend_serialnumber;
    }

    public function getPoAmendByCode($po_amend_serialnumber) {
        //$result=mysql_query("select po_serialnumber from po_header where isamend=1 and activestatus=1") or die(mysql_error());
        // $ret=array();
        // while()
        //{
        $query = "select * from po_amend_header where po_amend_serialnumber='$po_amend_serialnumber' and activestatus=1";
        $s = mysql_query($query) or die(mysql_error());
        $row = mysql_fetch_object($s);
        $temp = new VoAmendHeader();
        $temp->po_serialnumber_fk = (int) $row->po_serialnumber_fk;
        $temp->po_amend_serialnumber = (int) $row->po_amend_serialnumber;
        $temp->po_amend_id = $row->po_amend_id;
        $temp->supplier_serialnumber_fk = (int) $row->supplier_serialnumber_fk;
        $temp->po_issue_date = $row->po_issue_date;
        $temp->expected_datetime = $row->expected_datetime;
        $temp->total_qty = (int) $row->total_qty;
        $temp->shipment_charge = (double) $row->shipment_charge;
        $temp->po_status = $row->po_status;
        $temp->discount = (double) $row->discount;
        $temp->tax = (double) $row->tax;
        $temp->total_amt = (double) $row->total_amt;
        $temp->po_remarks = $row->po_remarks;
        $temp->po_closed_date = $row->po_closed_date;
        // $temp->isamend=(boolean)$row->isamend;
        $temp->cre_userid_fk = (int) $row->cre_userid_fk;
        $temp->cre_datetime = $row->cre_datetime;
        $temp->activestatus = (boolean) $row->activestatus;
        $query1 = "select po_amend_serialnumber_fk,enq_amend_po_serialnumber,enq_serialnumber_fk from enq_amend_po where po_amend_serialnumber_fk='$po_amend_serialnumber' and activestatus=1";
        $s1 = mysql_query($query1) or die(mysql_error());
        while ($row1 = mysql_fetch_object($s1)) {
            $temp->enq_serialnumber_fk[] = (int) $row1->enq_serialnumber_fk;
            $temp->enq_amend_po_serialnumber[] = (int) $row1->enq_amend_po_serialnumber;
            $temp->po_amend_serialnumber_fk[] = (int) $row1->po_amend_serialnumber_fk;
        }
        $query2 = "select po_amend_serialnumber_fk,po_amend_detserialnumber,pro_serialnumber_fk,pro_discount,pro_tax,pro_qty,unit_price,pro_total_price from po_amend_details where po_amend_serialnumber_fk='$po_amend_serialnumber' and activestatus=1";
        $s2 = mysql_query($query2) or die(mysql_error());
        while ($row2 = mysql_fetch_object($s2)) {
            $temp->po_amend_serialnumber_fk[] = (int) $row2->po_amend_serialnumber_fk;
            $temp->po_amend_detserialnumber[] = (int) $row2->po_amend_detserialnumber;
            $temp->pro_serialnumber_fk[] = (int) $row2->pro_serialnumber_fk;
            $temp->pro_discount[] = (double) $row2->pro_discount;
            $temp->pro_tax[] = (double) $row2->pro_tax;
            $temp->pro_qty[] = (int) $row2->pro_qty;
            $temp->unit_price[] = (double) $row2->unit_price;
            $temp->pro_total_price[] = (double) $row2->pro_total_price;
        }
        // $ret[]=$temp;
        mysql_free_result($s);
        mysql_free_result($s1);
        mysql_free_result($s2);
        return $temp;
    }

    public function getAllPoAmends() {
        $result = mysql_query("select po_serialnumber from po_header where isamend=1 and activestatus=1") or die(mysql_error());
        $ret = array();
        while ($res = mysql_fetch_object($result)) {
            $query = "select * from po_amend_header where po_serialnumber_fk='$res->po_serialnumber' and activestatus=1 order by po_amend_serialnumber limit 1";
            $s = mysql_query($query) or die(mysql_error());
            $row = mysql_fetch_object($s);
            if (!empty($row)) {
                $temp = new VoAmendHeader();
                $temp->po_serialnumber_fk = (int) $row->po_serialnumber_fk;
                $temp->po_amend_serialnumber = (int) $row->po_amend_serialnumber;
                $temp->po_amend_id = $row->po_amend_id;
                $temp->supplier_serialnumber_fk = (int) $row->supplier_serialnumber_fk;
                $temp->po_issue_date = $row->po_issue_date;
                $temp->expected_datetime = $row->expected_datetime;
                $temp->total_qty = (int) $row->total_qty;
                $temp->shipment_charge = (double) $row->shipment_charge;
                $temp->po_status = $row->po_status;
                $temp->discount = (double) $row->discount;
                $temp->tax = (double) $row->tax;
                $temp->total_amt = (double) $row->total_amt;
                $temp->po_remarks = $row->po_remarks;
                $temp->po_closed_date = $row->po_closed_date;
                $temp->cre_userid_fk = (int) $row->cre_userid_fk;
                $temp->cre_datetime = $row->cre_datetime;
                $temp->activestatus = (boolean) $row->activestatus;
                $query1 = "select po_amend_serialnumber_fk,enq_amend_po_serialnumber,enq_serialnumber_fk from enq_amend_po where po_amend_serialnumber_fk='$row->po_amend_serialnumber' and activestatus=1";
                $s1 = mysql_query($query1) or die(mysql_error());
                while ($row1 = mysql_fetch_object($s1)) {
                    $temp->enq_serialnumber_fk[] = (int) $row1->enq_serialnumber_fk;
                    $temp->enq_amend_po_serialnumber[] = (int) $row1->enq_amend_po_serialnumber;
                    $temp->po_amend_serialnumber_fk[] = (int) $row1->po_amend_serialnumber_fk;
                }
                $query2 = "select po_amend_serialnumber_fk,po_amend_detserialnumber,pro_serialnumber_fk,pro_discount,pro_tax,pro_qty,unit_price,pro_total_price from po_amend_details where po_amend_serialnumber_fk='$row->po_amend_serialnumber' and activestatus=1";
                $s2 = mysql_query($query2) or die(mysql_error());
                while ($row2 = mysql_fetch_object($s2)) {
                    $temp->po_amend_serialnumber_fk[] = (int) $row2->po_amend_serialnumber_fk;
                    $temp->po_amend_detserialnumber[] = (int) $row2->po_amend_detserialnumber;
                    $temp->pro_serialnumber_fk[] = (int) $row2->pro_serialnumber_fk;
                    $temp->pro_discount[] = (double) $row2->pro_discount;
                    $temp->pro_tax[] = (double) $row2->pro_tax;
                    $temp->pro_qty[] = (int) $row2->pro_qty;
                    $temp->unit_price[] = (double) $row2->unit_price;
                    $temp->pro_total_price[] = (double) $row2->pro_total_price;
                }
                $ret[] = $temp;
            }
            mysql_free_result($s);
            mysql_free_result($s1);
            mysql_free_result($s2);
        }

        return $ret;
    }

}

class VoAmendHeader {

    var $po_serialnumber_fk;
    var $po_amend_serialnumber;
    var $po_amend_id;
    var $supplier_serialnumber_fk;
    var $po_issue_date;
    var $expected_datetime;
    var $total_qty;
    var $shipment_charge;
    var $po_status;
    var $discount;
    var $tax;
    var $total_amt;
    var $po_remarks;
    var $po_closed_date;
    var $enq_serialnumber_fk;
    var $enq_amend_po_serialnumber;
    var $po_amend_serialnumber_fk;
    var $po_amend_detserialnumber;
    var $pro_serialnumber_fk;
    var $pro_discount;
    var $pro_tax;
    var $pro_qty;
    var $unit_price;
    var $pro_total_price;
    var $cre_userid_fk;
    var $cre_datetime;
    var $activestatus;

}

//$vo=new VoAmendHeader;
//$vo->po_amend_id="653";
//$vo->po_serialnumber_fk=2;
//$arr=array();
//$arr[0]=30;
//$arr[1]=33;
//$arr[2]=3;
//$vo->enq_serialnumber_fk=$arr;
//$aar=array();
//$aar[0]=1;
//$aar[1]=2;
//$aar[2]=3;
//$vo->pro_serialnumber_fk=$aar;
//$qty=array();
//$qty[0]=34;
//$qty[1]=45;
//$qty[2]=16;
//$vo->pro_qty=$qty;
//$tax=array();
//$tax[0]=11.23;
//$tax[1]=12.34;
//$vo->pro_tax=$tax;
//$sup=array();
//$sup[0]=43;
//$sup[1]=44;
//$vo->unit_price=$sup;
//$vo->supplier_serialnumber_fk=4;
//$vo->enq_sup_serialnumber=$sup;
//$qww=array();
//$qww[0]=373;
//$qww[1]=500;
//$vo->enq_detserialnumber=$qww;
//$vo->pro_qty=$qty;
//$dis=array();
//$dis[0]=12.45;
//$dis[1]=23.06;
//$vo->pro_discount=$dis;
//$vo->pro_total_price=$qww;
//$vo->total_qty=79;
//$rem=array();
//$rem[0]=13;
//$rem[1]=14;
//$vo->enq_po_serialnumber=$rem;
//$det=array();
//$det[0]=13;
//$det[1]=14;
//$vo->po_detserialnumber=$det;
//$vo->po_serialnumber=10;
//$rem[2]="qweqwee";
//$vo->pro_remarks=$rem;
//$vo->pro_serialnumber_fk=$aar;
//$vo->cre_userid_fk=1;
////////$vo->enq_serialnumber=33;
//$ss=new PoAmend;
//echo $ss->getAllPoAmends();
?>